package org.jiucheng.template.grammar;

import java.util.Map;

import org.jiucheng.template.GrammarManager;

public class Include implements Grammar {

    private String filePath;
    private GrammarFile grammarFile;
    private GrammarManager grammarManager;

    public Include(String filePath) {
        this.filePath = filePath;
    }

    public void setGrammarManager(GrammarManager grammarManager) {
        this.grammarManager = grammarManager;
    }

    public void setGrammarFile(GrammarFile grammarFile) {
        this.grammarFile = grammarFile;
    }

    public void exec(Map<String, Object> context, StringBuilder sb) {
        Root root;
        if (filePath.startsWith("/")) {
            root = grammarManager.parse(filePath);
        } else {
            String file = grammarFile.getFile();
            root = grammarManager.parse(file.substring(0, file.lastIndexOf("/") + 1) + filePath);
        }
        root.exec(context, sb);
    }
}
